// pages/activity/official.js
import { Activity } from "../../model/activity";
const appInst =  getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    hidden: false,
    pay_loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中'
    })
    this.getActivityInfo(options.id)
  },
  async getActivityInfo(id) {
    const res = await Activity.getGfActivityInfo(id)
    this.setData({
      info: res,
      hidden: true
    })
    wx.hideLoading()
  },
  /**
   * 创建官方活动订单
   */
  async createOrder() {
    const self = this;
    const id = this.data.info.id;
    this.setData({
      pay_loading: true,
    })
    const res = await Activity.createGfActivityOrder( id );
    if(res) {
      appInst.preOrder(res.out_trade_no,function (){
          self.changeLoading();
      });
    } else {
      this.setData({
        pay_loading: false,
      })
    }
  },
  /**
   * 关闭弹框以及加载
   */
  changeLoading() {
    this.setData({
      pay_loading: false,
    });
    wx.hideLoading();
  },
})
